package models

import (
	"go-admin-official/modules/database"
)

type PluginsGoodModel struct {
	Base `json:"-"`

	Id        int64  `json:"id"`
	PluginId  int64  `json:"plugin_id"`
	UserId    int64  `json:"user_id"`
	CreatedAt string `json:"created_at"`
}

func PluginsGood() PluginsGoodModel {
	return PluginsGoodModel{Base: Base{Table: "plugins_good"}}
}

func (t PluginsGoodModel) WithTx(tx *database.SqlTx) PluginsGoodModel {
	t.Tx = tx
	return t
}

func (t PluginsGoodModel) Find(id interface{}) PluginsGoodModel {
	item, _ := database.Table(t.Table).Find(id)

	if item == nil {
		return PluginsGood()
	}

	return t.MapToModel(item)
}

func (t PluginsGoodModel) Update(key string, value interface{}) (int64, error) {
	return database.Table(t.Table).
		Where("id", "=", t.Id).
		Update(database.H{
			key: value,
		})
}

func (t PluginsGoodModel) IsEmpty() bool {
	return t.Id == int64(0)
}

func (t PluginsGoodModel) IsNotEmpty() bool {
	return t.Id != int64(0)
}

func (t PluginsGoodModel) MapToModel(m map[string]interface{}) PluginsGoodModel {
	t.Id, _ = m["id"].(int64)
	t.PluginId, _ = m["plugin_id"].(int64)
	t.UserId, _ = m["user_id"].(int64)
	t.CreatedAt, _ = m["created_at"].(string)
	return t
}
